package com.fenghongzhang.music;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.Manifest;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.fenghongzhang.music.adapter.IndexFragmentAdapter;
import com.fenghongzhang.music.broadcast.MusicReceiver;
import com.fenghongzhang.music.fragment.BlankFragment;
import com.fenghongzhang.music.fragment.MusicFragment;
import com.fenghongzhang.music.util.BroadcastConst;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";


    private ViewPager vp;
    private RadioGroup rg;
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;
    private RadioButton rb4;



    private List<Fragment> fragmentList ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //初始化所有组件
        initView();


        //动态的方式来注册一个广播接受者
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BroadcastConst.PAUSE);
        intentFilter.addAction("com.feng.restart");
        MusicReceiver musicReceiver = new MusicReceiver();
        registerReceiver(musicReceiver,intentFilter);





        fragmentList = new ArrayList<>();


        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},100);



        //添加一个翻页的监听器
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                if(position == 0){
                    rb1.setChecked(true);
                }else if(position == 1){
                    rb2.setChecked(true);
                }else if(position == 2){
                    rb3.setChecked(true);
                }else if(position == 3){
                    rb4.setChecked(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        //跟句rg的点击来选择vp
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId ==  rb1.getId()){
                    vp.setCurrentItem(0);
                }else if(checkedId == rb2.getId()){
                    vp.setCurrentItem(1);
                }else if(checkedId == rb3.getId()){
                    vp.setCurrentItem(2);
                }else if(checkedId == rb4.getId()){
                    vp.setCurrentItem(3);
                }
            }
        });


    }

    private void initView() {
        vp = (ViewPager) findViewById(R.id.vp);
        rg = (RadioGroup) findViewById(R.id.rg);
        rb1 = (RadioButton) findViewById(R.id.rb1);
        rb2 = (RadioButton) findViewById(R.id.rb2);
        rb3 = (RadioButton) findViewById(R.id.rb3);
        rb4 = (RadioButton) findViewById(R.id.rb4);

    }

    //动态权限请求返回的结果
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            //同意
            fragmentList.add(new MusicFragment());
            for (int i = 0; i < 3; i++) {
                fragmentList.add(new BlankFragment());

            }
            IndexFragmentAdapter indexFragmentAdapter = new IndexFragmentAdapter(getSupportFragmentManager(), fragmentList);
            vp.setAdapter(indexFragmentAdapter);

        }else{
            finish();
        }
    }
}
